//
//  ViewController.m
//  UI16_UICollectionView
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "ViewController.h"




@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) MovieCollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *dataSourceArr;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"相册";
    [self createCollectionView];
    [self dataHandle];
}

- (void)createCollectionView {
    MovieLayout *layout = [[MovieLayout alloc] init];
    self.collectionView = [[MovieCollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor yellowColor];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    [self.collectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    /** 调用手势 */
    [self addLongPressGesture];
}


#pragma mark - 移动item
/** 哪些item可以移动 */
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

/** 移动item调用此方法 */
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    /** 更新数据源 */
}

/** 选中item后调用此方法 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SecondViewController *secVc = [[SecondViewController alloc] init];
    secVc.movieModelArr = self.dataSourceArr;
    secVc.firstIndexPath = indexPath;
    MovieCollectionViewCell *cell = (MovieCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    secVc.movieCell = cell;
    [self.navigationController pushViewController:secVc animated:YES];
    
}

- (void)addLongPressGesture {
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [self.collectionView addGestureRecognizer:longPress];
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    /** 根据长按手势的状态，调用item的移动方法 */
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
        {
            /** 根据手势在View上的点，找到对应的item的indexPath */
            NSIndexPath *beginIndexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];
            /** 如果没有点中item，直接退出 */
            if (beginIndexPath == nil) {
                return;
            }
            [self.collectionView beginInteractiveMovementForItemAtIndexPath:beginIndexPath];
            break;
        }
        case UIGestureRecognizerStateChanged:
        {
            /** 随着手势移动，item位置不断更新 */
            [self.collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:self.collectionView]];
            break;
        }
         case UIGestureRecognizerStateEnded:
        {
            /** 手势结束，item移动结束 */
            [self.collectionView endInteractiveMovement];
            break;
        }
        default:
        {
            [self.collectionView cancelInteractiveMovement];
        }
            break;
    }
    
}

- (void)dataHandle {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    self.dataSourceArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        movieModel *model = [[movieModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataSourceArr addObject:model];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    movieModel *model = self.dataSourceArr[indexPath.row];
    [cell setMovieModel:model];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
